
var QDevelop_Application_Domain = "/CityTraffic";
var QDevelop_JsonAction = QDevelop_Application_Domain+"/json/queryDB.action?";

/*
*	动态生成一个弹出窗口显示
*/
var moreInfoViewId = 0;
var moreInfoView = function(infoParam,_width,_height,subMethod,isWait,isLeft){
		var title;
		if(typeof infoParam == 'object'){
			title = infoParam.title;
			_width = infoParam.width;
			_height = infoParam.height;
			this.contentHtml = infoParam.content;
			subMethod = infoParam.subMethod;
			isWait = infoParam.isWait;
			isLeft = infoParam.isLeft;
		}else{
			title = infoParam.toString();
		}
		_width = _width==null?800:_width;
		_height = _height==null?400:_height;
		if(!isWait) _height = _height<90?120:_height;

		this.winId = "autoWin_"+moreInfoViewId;
		this.contengId = "autoContent_"+moreInfoViewId;
		this.contentHtml;
		this.width = _width;
		this.height = _height;
		this.title = title;
		this.isLeft = isLeft==null?false:isLeft;
		this.isWait = isWait;
		this.subMethod = subMethod;
		this.isInited = false;
		moreInfoViewId++;
}

moreInfoView.prototype.init = function(){
	if(!this.isInited){
		if(this.isWait&&this.contentHtml==null){
			this.contentHtml = '<IMG SRC="'+QDevelop_Application_Domain+'/css/05043110.gif">';
		}
		this.isInited = true;	
		$("body").append('<div id="'+this.winId+'" title="'+this.title+'" style="display:none;overflow:hidden;width:'+this.width+';height:'+this.height+
			'"><div id="'+this.contengId+'" style="width:'+(this.width-19)+';overflow-y:scroll;overflow-x:hidden;height:'+
			(this.isWait?(this.height-10):(this.height-90))+(this.isLeft?'':';text-align:center')+';"></div></div>');
		
		if(this.subMethod){
			$("#"+this.winId).dialog({
					autoOpen: false,
					width:this.width,
					height:this.height,
					bgiframe: true,
					modal: true,
					buttons: {
					"  提  交  ": this.subMethod,
					"  关  闭  ": function() { 
						$(this).dialog("close"); 
					} 
				}
			});
		}else{
			if(this.isWait==null||!this.isWait){
				$("#"+this.winId).dialog({
						autoOpen: false,
						width:this.width,
						height:this.height,
						bgiframe: true,
						modal: true,
						buttons: {
						"  关  闭  ": function() { 
							$(this).dialog("close"); 
						} 
					}
				});
			}else{
				$("#"+this.winId).dialog({
						autoOpen: false,
						width:this.width,
						height:this.height,
						bgiframe: true,
						modal: true
				});
			}
		}
	}
}

moreInfoView.prototype.setContent = function(content){
	if(typeof content == 'string')
		this.contentHtml = content;
	else{
		this.contentHtml = content.html();
		content.remove();
	}
}

moreInfoView.prototype.show = function(content){
	this.init();
	$("#"+this.winId).dialog('open');
	if(content==null&&this.contentHtml != null){
			$('#'+this.contengId).html(this.contentHtml);		
	}
	if(content==null) return;
	if(typeof content == 'string'){
		$('#'+this.contengId).html(content);
	}else{
		if(this.contentHtml == null){
			this.contentHtml = content.html();
			content.remove();
		}
		$('#'+this.contengId).html(this.contentHtml);
	}
}

moreInfoView.prototype.showHidden = function(){
	this.show();
	$("#"+this.winId).hide();
}


moreInfoView.prototype.close = function(){
	$("#"+this.winId).dialog('close');
}

moreInfoView.prototype.destory = function(){
	$("#"+this.winId).remove();
}


var wait= new moreInfoView('数据正在提交，请稍后... ...',280,50,null,true);


var Pagination = function(data,clickMethod,showDiv){
		this.paginationData = data;
		this.clickMethod = clickMethod;
		this.showDiv = showDiv;
}

Pagination.prototype.show = function(page,maxNum,_n){
	var maxShowNum = 8;
	if(this.paginationData==null || typeof this.paginationData != 'object') return;
	var minId =(page-1)*maxNum;
	var maxId = page*maxNum;
	var idx = 0;
	var tmp = '<table class="customTable">';
	for(each in this.paginationData){
		if(idx<=maxId && idx>=minId){
			if(idx%2==0)
				tmp += '<tr onclick="changeCss(this);'+this.clickMethod+'(\''+this.paginationData[each]+'\',\''+each+'\');"><td>'+each+'</td></tr>';
			else
				tmp += '<tr class="anthor_tr" onclick="changeCss(this);'+this.clickMethod+'(\''+this.paginationData[each]+'\',\''+each+'\');"><td >'+each+'</td></tr>';
		}
		idx ++;
	}
	tmp += '<table>';
	if(page>1 || idx>maxNum){
		var allPage = idx%maxNum==0?idx/maxNum:(1+parseInt(idx/maxNum));
		var p_temp = '';
		var nowClass = "";
		var m_1,m_2;
		var isSplit = allPage>maxShowNum?true:false;
		if(isSplit){
			m_1 = parseInt(page-(maxShowNum-2)/2);
			m_2 = parseInt(page+(maxShowNum-2)/2);
			if(m_1<2){m_2 = m_2 + Math.abs(m_1)+1;m_1 = 2;	}
			var ff = (allPage-m_2) - parseInt((maxShowNum-2)/2)  ;
			if(ff<0){m_1 +=  ff;m_2 = allPage-1;	}
		}
		var tmpP = '';
		for(var i=1;i<=allPage;i++){
			nowClass = (i==page? "hot":"common");
			if(isSplit){
				if(i==1||i==allPage||(i>=m_1&&i<=m_2)){
					p_temp += tmpP+'<a class="'+nowClass+'" href="javascript:'+_n+'.show('+i+','+maxNum+',\''+_n+'\')">'+i+'</a>&nbsp;';
					tmpP='';
				}else{
					tmpP = '...&nbsp;';
				}
			}else{
				p_temp += '<a class="'+nowClass+'" href="javascript:'+_n+'.show('+i+','+maxNum+',\''+_n+'\')">'+i+'</a>&nbsp;';
			}
		}
		tmp += '<div style="width:100%;text-align:center;">'+p_temp+'</div>';
	}
	$('#'+this.showDiv).html(tmp);
}

var lastObj=null;
var changeCss = function(obj){
	if(lastObj!=null){
		$(lastObj).css({'background-color':'','color':''});
	}
	$(obj).css({'background-color':'#25569E','color':'#ffffff'});
	lastObj = obj;
}


/*	
	QueryTable({
		div:'.leftFrameCss',
		css:'text-align:center;width:400px',
		queryParam:'index=business_group_relation_view&bName=feixin&page=1&maxNum=5',
		title:null,
		elemt:{
			'群组名':{key:'GID1',order:true},
			'共有人数':{key:'JOIN_NUM',order:true},
			'前向覆盖率':{key:'FORWARD_COVER',order:true,method:function(data){return data+'%';}}
		}
	});	
	*/
var QueryTable_PARAM={};
var QueryTable = function(param){
	if(param.queryParam == null || param.elemt == null) return;
	var paramKey = (Math.random()).toString();
	QueryTable_PARAM[paramKey] = param;
	wait.show();	
	var showOrderStat = function(title,key){
		if(param.queryParam.indexOf('order='+key)>-1){
			if(param.queryParam.indexOf('order='+key+' desc')>-1){
				return title+'↓';
			}else
				return title+'↑';
		}
		return title;
	}
	$.getJSON(QDevelop_JsonAction+param.queryParam,function(data){	
		var tmpHtml = '';
		tmpHtml += (param.title==null?'':param.title);
		tmpHtml += ('<div '+(param.css==null?'':'style="'+param.css+'"')+'><table class="customTable">');
		var tmpTitle = '';
		var elemNumer = 0;
		for(each in param.elemt){
			elemNumer++;
			if(param.elemt[each].order)
				tmpTitle += '<th><a href="javascript:_order(\''+paramKey+'\',\''+param.elemt[each].key+'\')">'+showOrderStat(each,param.elemt[each].key)+'</a></th>';
			else
				tmpTitle += '<th>'+showOrderStat(each,param.elemt[each].key)+'</th>';
		}
		tmpHtml += ('<tr>'+tmpTitle+'</tr>');
		if(data!=null&&data.json.resultList!=null&&data.json.resultList.length>0){
			var _rb;
			for(var i=0;i<data.json.resultList.length;i++){
				_rb = data.json.resultList[i];
				var tmpVale = '';
				for(each in param.elemt){
					if(param.elemt[each].method)
						tmpVale += '<td>'+param.elemt[each].method(_rb[param.elemt[each].key])+'</td>';
					else if(param.elemt[each].describe)
						tmpVale += '<td>'+param.elemt[each].describe(_rb)+'</td>';
					else 
						tmpVale += '<td>'+_rb[param.elemt[each].key]+'</td>';
				}
				tmpHtml += ('<tr'+ ((i>0&&i%2==0)?'class="anthor_tr"':'')+'>'+tmpVale+'</tr>');
			}	
		}else{
			tmpHtml += '<tr><td colspan="'+elemNumer+'">对不起,查询咱无数据...</td></tr>';
		}
		tmpHtml += ('</table>');
		if(data!=null&&data.json.lastPage>1){
			if(param.pagination ==null || param.pagination =='CLASSIC'){
			var tmpPageOption = '';
			for(var i=1;i<=data.json.lastPage;i++){
				tmpPageOption += '<option value="'+i+'" '+(i==data.json.nowPage?'selected':'')+'>'+i+'</option>';
			}
			var tmpMaxNumOption ='';
			var _PageMaxNumCollection = [5,10,15,20,30,50,70,100];
			for(var i=0;i<_PageMaxNumCollection.length;i++){
				tmpMaxNumOption += '<option value="'+_PageMaxNumCollection[i]+'" '+
					(_PageMaxNumCollection[i]==data.json.maxNum?'selected':'')+'>'+_PageMaxNumCollection[i]+'</option>';
			}
			tmpHtml += '<table class="customTablePagination"><tr><td width="30%" nowrap>共'+data.json.allCount+'条记录 第'+
				data.json.nowPage+'/'+data.json.lastPage+'页</td><td width="40%" nowrap><A HREF="javascript:_goPage(\''+paramKey+'\','+
				data.json.firstPage+')">首页</A><A HREF="javascript:_goPage(\''+paramKey+'\','+data.json.previousPage+
				')">上一页</A><A HREF="javascript:_goPage(\''+paramKey+'\','+data.json.nextPage+')">下一页</A><A HREF="javascript:_goPage(\''+paramKey+'\','+
				data.json.lastPage+')">末页</A></td><td width="30%" nowrap>转到<SELECT onchange="javascript:_goPage(\''+paramKey+'\',this.options[this.selectedIndex].value)">'+
				tmpPageOption+'</SELECT>页</td><td nowrap>每页<select onchange="javascript:_setMaxNum(\''+paramKey+'\',this.options[this.selectedIndex].value)">'+
				tmpMaxNumOption+'</select>条</td></tr></table>';
			}else if(param.pagination =='PAGE_NUMBER'){
					var maxShowNum = 8;
					var p_temp = '';
					var nowClass = "";
					var m_1,m_2;
					var isSplit = data.json.lastPage>maxShowNum?true:false;
					if(isSplit){
						m_1 = parseInt(data.json.nowPage-(maxShowNum-2)/2);
						m_2 = parseInt(data.json.nowPage+(maxShowNum-2)/2);
						if(m_1<2){m_2 = m_2 + Math.abs(m_1)+1;m_1 = 2;	}
						var ff = (data.json.lastPage-m_2) - parseInt((maxShowNum-2)/2)  ;
						if(ff<0){m_1 +=  ff;m_2 = data.json.lastPage-1;}
					}
					var tmpP = '';
					for(var i=1;i<=data.json.lastPage;i++){
						nowClass = i==data.json.nowPage? "hot":"common";
						if(isSplit){
							if(i==1||i==data.json.lastPage||(i>=m_1&&i<=m_2)){
								p_temp += tmpP+'<a class="'+nowClass+'" href="javascript:_goPage(\''+paramKey+'\','+i+')">'+i+'</a>&nbsp;';
								tmpP='';
							}else{
								tmpP = '...&nbsp;';
							}
						}else{
							p_temp += '<a class="'+nowClass+'" href="javascript:_goPage(\''+paramKey+'\','+i+')">'+i+'</a>&nbsp;';
						}
					}
					tmpHtml += '<div style="width:100%;text-align:center;">'+p_temp+'</div>';
			}
		}
		wait.close();
		$(param.div).html(tmpHtml+'</div>');
	});
}

var _order = function(paramKey,_orderName){
	var focusUrl = QueryTable_PARAM[paramKey].queryParam;
	var _isDesc = false;
	if(focusUrl.indexOf('order='+_orderName)>-1&&focusUrl.indexOf('order='+_orderName+' desc')>-1){
			_isDesc = true;
	}
	var tmp = focusUrl.replace(/[?|\&amp;]order=([a-z|A-Z|\_|\.|\s|0-9|desc]*)/g,'');
	if(_isDesc){
		QueryTable_PARAM[paramKey].queryParam = tmp+'&order='+_orderName;
	}else{
		QueryTable_PARAM[paramKey].queryParam = tmp+'&order='+_orderName+' desc';
	}
	QueryTable(QueryTable_PARAM[paramKey]);
}

var _goPage = function(paramKey,num){
	var tmpParam = QueryTable_PARAM[paramKey].queryParam.replace(/[?|\&amp;]page=([0-9]*)/g,'');		
	QueryTable_PARAM[paramKey].queryParam = tmpParam+'&page='+num;
	QueryTable(QueryTable_PARAM[paramKey]);
}

var _setMaxNum = function(paramKey,num){
	var focusUrl = QueryTable_PARAM[paramKey].queryParam;
	var tmp = focusUrl.replace(/[?|\&amp;]maxNum=([0-9]*)/g,'');
	tmp = tmp.replace(/[?|\&amp;]page=([0-9]*)/g,'');
	QueryTable_PARAM[paramKey].queryParam = tmp+'&page=1&maxNum='+num;
	QueryTable(QueryTable_PARAM[paramKey]);
}
